﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-05-18 18:23
    /// 描 述：业务类
    /// </summary>
    public class ShopKfBLL
    {
        private ShopKfService shopKfService = new ShopKfService();

        #region 获取数据
        public async Task<TData<List<ShopKfEntity>>> GetList(ShopKfListParam param)
        {
            TData<List<ShopKfEntity>> obj = new TData<List<ShopKfEntity>>();
            obj.Data = await shopKfService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<ShopKfEntity>>> GetPageList(ShopKfListParam param, Pagination pagination)
        {
            TData<List<ShopKfEntity>> obj = new TData<List<ShopKfEntity>>();
            obj.Data = await shopKfService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<ShopKfEntity>> GetEntity(long id)
        {
            TData<ShopKfEntity> obj = new TData<ShopKfEntity>();
            obj.Data = await shopKfService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(ShopKfEntity entity)
        {
            TData<string> obj = new TData<string>();

            if (entity.shopids.IsEmpty())
            {
                obj.Message ="请选择门店!";
                obj.Tag = 0;
                return obj;
            }

            //调整判断链接是否被绑定
            var shopids=TextHelper.SplitToArray<long>(entity.shopids, ',');

            string msg = "";

            foreach (long shopid in shopids) {
                var shopkf=shopKfService.BaseRepository().FindList<ShopKfEntity>(t => t.shopids.Contains(shopid.ToString()) && t.Id!=entity.Id).Result.FirstOrDefault();
                if (shopkf != null) {
                    var shop= shopKfService.BaseRepository().FindEntity<ShopEntity>(shopid).Result;
                    if (shop!=null)
                    {
                        msg += shop.shop_name + ",";
                    }
                }
            }

            if (!msg.IsEmpty())
            {
                msg= msg.Substring(0, msg.Length - 1);
                obj.Message = msg;
                obj.Tag =0;
                return obj;
            }

            await shopKfService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await shopKfService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
